package v1

import (
	bll "aggregation/bll/dashboard"
	model "aggregation/model/dashboard"
	"auth/utils"
	"strings"

	"github.com/gin-gonic/gin"
)

type device struct {
}

var Device = &device{}

func (d *device) Init(r *gin.RouterGroup) {
	router := r.Group("/dashboard/device") //, middleware.Auth())
	{
		router.GET("/board", d.deviceBoard)
		router.GET("/wave", d.deviceBindWave)
		router.GET("/lost/board", d.lostBoard)
		router.GET("/lost/wave", d.lostWave)
		router.GET("/fault/board", d.faultBoard)
		router.GET("/fault/wave", d.faultWave)
		router.GET("/summary", d.deviceSummary)
	}
}

func (d *device) deviceBoard(ctx *gin.Context) {
	var (
		board model.DeviceBoardResponse
		err   error
	)
	if board, err = bll.Device.DeviceBoard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, board)
}

func (d *device) deviceBindWave(ctx *gin.Context) {
	var (
		period string
		wave   model.DeviceBindWaveResponse
		err    error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))
	if wave, err = bll.Device.DeviceBindWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, wave)
}

func (d *device) lostBoard(ctx *gin.Context) {
	var (
		board model.DeviceLostBoardResponse
		err   error
	)
	if board, err = bll.Device.DeviceLostBoard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, board)
}
func (d *device) lostWave(ctx *gin.Context) {
	var (
		period string
		wave   model.DeviceLostWaveResponse
		err    error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))
	if wave, err = bll.Device.DeviceLostWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, wave)
}
func (d *device) faultBoard(ctx *gin.Context) {
	var (
		board model.DeviceFaultBoardResponse
		err   error
	)
	if board, err = bll.Device.DeviceFaultBoard(ctx.Request.Context()); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, board)
}
func (d *device) faultWave(ctx *gin.Context) {
	var (
		period string
		wave   model.DeviceFaultWaveResponse
		err    error
	)
	period = strings.TrimSpace(strings.ToLower(ctx.DefaultQuery("period", "day")))
	if wave, err = bll.Device.DeviceFaultWave(ctx.Request.Context(), model.PERIOD(period)); err != nil {
		ctx.Error(err)
		return
	}
	utils.ResponseOK(ctx, wave)
}

func (d *device) deviceSummary(ctx *gin.Context) {
	var summary model.DeviceSummaryResponse
	summary = bll.Device.DeviceSummary(ctx.Request.Context())
	utils.ResponseOK(ctx, summary)
}
